package cn.edu.njtc.service.impl;

import cn.edu.njtc.entity.pojo.Transaction;
import cn.edu.njtc.mapper.TransactionMapper;
import cn.edu.njtc.service.TransactionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class TransactionServiceImpl extends ServiceImpl<TransactionMapper, Transaction> implements TransactionService {

    @Override
    public Transaction recharge(Integer userId, BigDecimal amount) {
        Transaction transaction = new Transaction();
        transaction.setUserId(userId);
        transaction.setTransactionType("充值");
        transaction.setAmount(amount);
        transaction.setTransactionDate(LocalDateTime.now());
        transaction.setStatus("成功");
        transaction.setNotes("会员充值");
        transaction.setDeleted(false);

        this.save(transaction);
        return transaction;
    }
    @Override
    public Transaction consume(Integer userId, BigDecimal amount, String notes) {
        Transaction transaction = new Transaction();
        transaction.setUserId(userId);
        transaction.setTransactionType("消费");
        transaction.setAmount(amount);
        transaction.setTransactionDate(LocalDateTime.now());
        transaction.setStatus("成功");
        transaction.setNotes(notes);
        transaction.setDeleted(false);

        this.save(transaction);
        return transaction;
    }


    @Override
    public List<Transaction> getTransactionsByUserId(Integer userId) {
        return this.lambdaQuery()
                .eq(Transaction::getUserId, userId)
                .eq(Transaction::getDeleted, false)
                .orderByDesc(Transaction::getTransactionDate)
                .list();
    }
}